package com.qf.security;

import com.qf.entity.TlPerm;
import com.qf.entity.TlUser;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

//自定义类：重写认证的实现
@Data
public class SecurityUser implements UserDetails {

    private TlUser loginUser;//登录用户
    private List<TlPerm> permissionList;//查询到的所有权限
    //权限
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //String collect = permissionList.stream().map(p -> p.getPermId().toString()).collect(Collectors.joining(","));
        //List<GrantedAuthority>
        return permissionList.stream().map(p->new SimpleGrantedAuthority(p.getPermId().toString())).collect(Collectors.toList());
//        return null;
    }

    @Override
    public String getPassword() {
        return loginUser.getUserPass();
    }

    @Override
    public String getUsername() {
        return loginUser.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
